package com.fivetalks.client;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fivetalks.object.ChatMessage;

public class ChatCustomAdapter extends ArrayAdapter<ChatMessage> implements
		OnClickListener {

	private Bitmap bm;
	private Context mContext;
	private int mResource;
	private ArrayList<ChatMessage> mList;
	private LayoutInflater mInflater;
	private String str = null;

	public ChatCustomAdapter(Context context, int layoutResource,
			ArrayList<ChatMessage> objects, String str) {
		super(context, layoutResource, objects);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.str = str;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ChatMessage chat = mList.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(mResource, null);
		}

		if (chat != null) {

			ImageView ivImage = (ImageView) convertView
					.findViewById(R.id.ivImage);
			TextView tvNickname = (TextView) convertView
					.findViewById(R.id.tvNickname);
			TextView tvMessage = (TextView) convertView
					.findViewById(R.id.tvMessage);

			if ("ChattingActivity".equals(str))
			{
				Log.d("custom","===mask_"+chat.getId());
				ivImage.setImageResource(mContext.getResources().getIdentifier(
						"mask_" + chat.getId(), "drawable",
						mContext.getPackageName()));
			}
			else
			{
				Log.d("custom","===face_"+chat.getId());
				ivImage.setImageResource(mContext.getResources().getIdentifier(
						"face_" + chat.getId(), "drawable",
						mContext.getPackageName()));
			}
				tvNickname.setText(chat.getNickName());
			tvMessage.setText(chat.getMessage());
		}

		convertView.setTag(position);
		convertView.setOnClickListener(this);

		return convertView;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
}
